package com.gitee.kamismile.igniteEx.spring.mapper;

import com.gitee.kamismile.igniteEx.common.IginteConfigProperties;
import com.gitee.kamismile.igniteEx.ignite.ServiceHandle;
import com.gitee.kamismile.igniteEx.spring.binding.MapperRegistry;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

public class MapperFactoryBean<T> implements FactoryBean<T>, InitializingBean {

    private Class<T> mapperInterface;
    private IginteConfigProperties iginteConfigProperties;
    private MapperRegistry mapperRegistry = new MapperRegistry();
    private ServiceHandle serviceHandle;

    public MapperFactoryBean() {
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        mapperRegistry.addMapper(mapperInterface);
    }

    @Override
    public T getObject() throws Exception {
        return mapperRegistry.getMapper(mapperInterface, serviceHandle, iginteConfigProperties);
    }

    @Override
    public Class<T> getObjectType() {
        return this.mapperInterface;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public Class<T> getMapperInterface() {
        return mapperInterface;
    }

    public void setMapperInterface(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public IginteConfigProperties getIginteConfigProperties() {
        return iginteConfigProperties;
    }

    public void setIginteConfigProperties(IginteConfigProperties iginteConfigProperties) {
        this.iginteConfigProperties = iginteConfigProperties;
    }

    public MapperRegistry getMapperRegistry() {
        return mapperRegistry;
    }

    public void setMapperRegistry(MapperRegistry mapperRegistry) {
        this.mapperRegistry = mapperRegistry;
    }

    public ServiceHandle getServiceHandle() {
        return serviceHandle;
    }

    public void setServiceHandle(ServiceHandle serviceHandle) {
        this.serviceHandle = serviceHandle;
    }
}
